package com.xiaomiMall.back.service.Impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.xiaomiMall.back.domain.Member;
import com.xiaomiMall.back.mapper.MemberMapper;
import com.xiaomiMall.back.service.MemberService;
import com.xiaomiMall.mange.domain.MallUser;
import com.xiaomiMall.mange.domain.vo.Notepad;
import com.xiaomiMall.mange.service.Impl.MallUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    MemberMapper memberMapper;

    @Autowired
    MallUserServiceImpl userService;

    /**
     * 查询会员对象list
     * @return 结果
     */
    @Override
    public List<Member> getAllMember() {
        return memberMapper.getAllMember();
    }

    /**
     * 由发送人id获取申请消息
     * @param senderId 发送人id
     * @return 结果
     */
    @Override
    public List<Notepad> selectNotepadBySenderId(Long senderId) {
        return memberMapper.selectNotepadBySenderId(senderId);
    }

    /**
     * 由消息id获取消息通知
     * @param id 消息id
     * @return 结果
     */
    @Override
    public Notepad selectNotepadByid(Long id) {
        return memberMapper.selectNotepadByid(id);
    }

    /**
     * 更新消息通知状态
     * @param id  消息通知的id
     * @param status 新的状态
     * @return 结果
     */
    @Override
    public Boolean updateNotepadStatus(Long id, Long status) {
        int i = 0;
        Notepad notepad = selectNotepadByid(id);
        if (notepad.getStatus() == 0) {
            i = memberMapper.updateNotepadStatus(id,status);
        }
        return i>0;
    }

    /**
     * 更新用户的钱包余额（即同意充值）
     * @param userId 用户id
     * @param wallet 用户余额
     * @return 结果
     */
    @Override
    public Boolean updateUserWallet(Long userId, int wallet) {
        MallUser mallUser = userService.selectMallUserById(userId);
        int restAmount = mallUser.getWallet() + wallet;
        int i = memberMapper.updateUserWallet(userId, restAmount);
        return i > 0;
    }

    /**
     * 重置商城用户的密码
     * @param userId     商城用户id
     * @return 结果
     */
    @Override
    public String resetPassword(Long userId) {
        int pass = (int)(Math.random()*100000000);
        String str = Integer.toString(pass);
        String rePass = SecurityUtils.encryptPassword(str);
        if (memberMapper.updatePassword(userId, rePass) > 0) {
            return str;
        };
        return null;
    }

    /**
     * 邮箱查询会员
     * @param email 邮箱
     * @return 结果
     */
    @Override
    public List<Member> selectMemberByEmail(String email) {
        return memberMapper.selectMemberByEmail(email);
    }
}
